package com.wits.qso.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import com.wits.platform.service.BaseService;
import com.wits.platform.web.mvc.PageParam;
import com.wits.qso.dao.HWKAttachmentDao;
import com.wits.qso.dao.HomeworkDao;
import com.wits.qso.dao.HomeworkHDao;
import com.wits.qso.dao.HomeworkHViewDao;
import com.wits.qso.dao.StudentDao;
import com.wits.qso.dao.TeacherDao;
import com.wits.qso.entity.HWKAttachment;
import com.wits.qso.entity.Homework;
import com.wits.qso.entity.HomeworkHView;

/**
 * 缴交作业流程服务
 */
@Component
public class HomeworkService extends BaseService<Homework, String> {

	/**
	 * 缴交作业数据库操作类
	 */
	private HomeworkDao homeworkDao;
	/**
	 * 布置作业数据库操作类
	 */
	@Resource
	private HomeworkHDao homeworkHDao;
	/**
	 * 缴交作业附件数据库操作类
	 */
	@Resource
	private HWKAttachmentDao hwkAttachmentDao;

	/**
	 * 老师数据库操作类
	 */
	@Resource
	private TeacherDao teacherDao;
	/**
	 * 学生数据库操作类
	 */
	@Resource
	private StudentDao studentDao;

	/**
	 * 作业视图查询数据库操作类
	 */
	@Resource
	private HomeworkHViewDao homeworkHViewDao;

	@Resource
	public void setHomeworkDao(HomeworkDao homeworkDao) {
		this.baseDao = homeworkDao;
		this.homeworkDao = homeworkDao;
	}

	public HomeworkDao getHomeworkDao() {
		return homeworkDao;
	}

	public HomeworkHDao getHomeworkHDao() {
		return homeworkHDao;
	}

	public void setHomeworkHDao(HomeworkHDao homeworkHDao) {
		this.homeworkHDao = homeworkHDao;
	}

	public HWKAttachmentDao getHwkAttachmentDao() {
		return hwkAttachmentDao;
	}

	public void setHwkAttachmentDao(HWKAttachmentDao hwkAttachmentDao) {
		this.hwkAttachmentDao = hwkAttachmentDao;
	}

	public TeacherDao getTeacherDao() {
		return teacherDao;
	}

	public void setTeacherDao(TeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}

	public StudentDao getStudentDao() {
		return studentDao;
	}

	public void setStudentDao(StudentDao studentDao) {
		this.studentDao = studentDao;
	}

	public HomeworkHViewDao getHomeworkHViewDao() {
		return homeworkHViewDao;
	}

	public void setHomeworkHViewDao(HomeworkHViewDao homeworkHViewDao) {
		this.homeworkHViewDao = homeworkHViewDao;
	}

	/**
	 * 根据参数查询作业
	 * 
	 * @param params
	 * @return
	 */
	public List<Homework> findHomeworks(Map<String, Object> params) {
		return homeworkDao.findHomeworks(params);
	}

	/**
	 * 根据参数查询作业视图记录
	 * 
	 * @param params
	 * @return
	 */
	public List<HomeworkHView> findHomeworkHViews(Map<String, Object> params) {
		return homeworkHViewDao.findHomeworkHViews(params);
	}
	/**
	 * 分页查询根据参数查询作业视图记录
	 * 
	 * @param params
	 * @return
	 */
	public Page<HomeworkHView> findHomeworkHViewPages(Map<String, Object> params,PageParam page) {
		Map<String,Object> param=new HashMap<String,Object>();
		param.putAll(params);
		param.put("orderByArray", new String[]{"hwkh_id"});
		param.put("pageNumber", page.getPageNum()); 
		param.put("pageSize", page.getNumPerPage());
		
		return homeworkHViewDao.findHomeworkHViewPages(param);
	}

	/**
	 * 根据Id查找附件
	 * 
	 * @param id
	 * @return
	 */
	public HWKAttachment getOneAttachment(String id) {
		return hwkAttachmentDao.findOne(id);
	}

	/**
	 * 删除一个附件
	 * @param id
	 * @return
	 */
	public boolean deleteOneAttachment(String id){
		boolean flag=false;
		try{
			hwkAttachmentDao.delete(id);
			flag=true;
		}catch(Exception e){
			e.printStackTrace();
			flag=false;
		}
		return flag;
	}
	/**
	 * 保存单个的附件
	 * @param hwkAttachment
	 * @return
	 */
	public boolean saveOneAttachment(HWKAttachment hwkAttachment){
		boolean flag=false;
		try{
			hwkAttachmentDao.save(hwkAttachment);
			flag=true;
		}catch(Exception e){
			e.printStackTrace();
			flag=false;
		}
		return flag;
	}
	/**
	 * 缴交作业
	 * 
	 * @param id
	 * @return
	 */
	public boolean submitHomework(String id) {
		// 缴交作业，状态置为待批HI
		return homeworkDao.modifyStatus(id, "HI");
	}

	/**
	 * 批改作业
	 * 
	 * @param homework
	 * @return
	 */
	public boolean correctHomework(Homework homework) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("hwk_id", homework.getId());
		param.put("hwk_cmmt", homework.getHwkComment());
		param.put("hwk_grade", homework.getHwkGrade());
		param.put("hwk_desc", "");
		param.put("hwk_status", homework.getHwkStatus());
		param.put("date_correct", new Date());
		return homeworkDao.modifyFeild(param);
	}
}
